package guan.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class WebHtmlRead {
	public static void main(String[] args) throws IOException {
		// String s = "/a/a/a/a";
		// System.out.println(s.substring(s.lastIndexOf("/") + 1, s.length()));
//		String a="a b c".replace(" ", "");
//		System.out.println(a);
		WebHtmlRead whr = new WebHtmlRead();
		whr.changeNameInFilePath();
	}

	public List<Video> listFromUrl() throws IOException {
		Document doc = Jsoup
				.connect(
						"http://v.163.com/special/opencourse/buildingdynamicwebsites.html")
				.get();
		String title = doc.title();
		Element table = doc.getElementById("list1");
		Elements es = table.getElementsByClass("u-ctitle");
		List<Video> videoList = new ArrayList<Video>();
		for (Iterator<Element> it = es.iterator(); it.hasNext();) {
			Element el = it.next();
			Element pre = el.nextElementSibling();
			Video video = new Video();
			video.setName(el.text());
			video.setUrl(pre.getElementsByTag("a").iterator().next()
					.attr("href"));
			videoList.add(video);
		}
		for (Video v : videoList) {
			System.out.println(v.getName());
		}
		for (Video v : videoList) {
			System.out.println(v.getUrl());
		}
		return videoList;

	}

	public List<File> listFilePath() {
		File path = new File("/Users/gjybst/Downloads/Music");
		List<File> fileList = new ArrayList<File>();
		for (String s : path.list()) {
			File tmp = new File(path.getPath() + "/" + s);
			if (tmp.exists()) {
				fileList.add(tmp);
			}
		}
		// System.out.println(fileList.size());
		return fileList;
	}

	public void changeNameInFilePath() throws IOException {
		File basePath = new File("/Users/gjybst/Downloads/Music/Web");
		if (!basePath.exists()) {
			basePath.mkdir();
		}
		List<Video> videoList = this.listFromUrl();
		List<File> fileList = this.listFilePath();
		for (File file : fileList) {
			for (Video video : videoList) {
				String url = video.getUrl();
				if (url.substring(url.lastIndexOf("/") + 1, url.length())
						.equals(file.getName())) {
					System.out.println(video.getUrl());
					System.out.println(file.getName());
					this.changeFileName(file, video);
				}
			}
		}
	}

	private void changeFileName(File file, Video video) throws IOException {
		String[] cmd = new String[] { "/bin/sh", "-c", " mv " };
		String s = cmd[cmd.length - 1] + " " + "/Users/gjybst/Downloads/Music/"
				+ file.getName() + " /Users/gjybst/Downloads/Music/Web/"
				+ video.getName().replace(" ", "")+".mp4";
		cmd[cmd.length - 1] = s;
		System.out.println(s);
		Process p = Runtime.getRuntime().exec(cmd);
		BufferedReader br = new BufferedReader(new InputStreamReader(
				p.getInputStream(), "utf8"));
		String str = null;
		while ((str = br.readLine()) != null) {
			System.out.println(str);
		}
		br.close();
	}
}

class Video implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String url;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
